package com.example.smart_campus.controller;


import com.example.smart_campus.common.Res;
import com.example.smart_campus.pojo.entity.CourseSelection;
import com.example.smart_campus.pojo.entity.Courses;
import com.example.smart_campus.service.CoursesService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程表，存储课程的基本信息 前端控制器
 * </p>
 *
 * @author liyv
 * @since 2024-10-19
 */
@RestController
@RequestMapping("/courses")
public class CoursesController {


    @Autowired
    CoursesService coursesService;

    /**
     * 录入课程
     */
    @PostMapping("/addCourse")
    public Res addCourse(@RequestBody Map<String, Object> requestData) {
        // public Res addCourse(String courseNo, String courseName, Integer credits, Integer teacherId, String courseIntroduction ){
        String courseNo = (String) requestData.get("courseNo");
        String courseName = (String) requestData.get("courseName");

        Object creditsObj = requestData.get("credits");
        float credits = ((Number) creditsObj).floatValue();

      //  System.out.println(credits);
       // float credits = (float)requestData.get("credits");
        //导致 ClassCastException，
        //因为 requestData.get("credits") 返回的是一个 Object，并不能直接转换为 float
        int teacherId = Integer.parseInt(requestData.get("teacherId").toString());
        String courseIntroduction = (String) requestData.get("courseIntroduction");

        coursesService.insertCourse(courseNo, courseName, credits, teacherId, courseIntroduction);
        return Res.ok();
    }


    /**
     * 选课设置：最大可选人数、选开始时间、选课结束时间
     */
    @PostMapping("/updateCourse")
    public Res updateCourse(@RequestBody Courses courses) {
        coursesService.updateCourse(courses);
        System.out.println(courses);
        return Res.ok(courses);
    }

    /**
     * 已选课程列表
     */
    @GetMapping("/getChosen")
    public Res getChosen(@RequestParam("studentId") Integer studentId) {
        return Res.ok(coursesService.chosenCourse(studentId));
    }

    /**
     * 可选课程列表
     */
//    @GetMapping("/getSelectCourse")
//    public Res getSelectCourses() {
//
//        return Res.ok(coursesService.getSelectCourses());
//    }


    /**
     * 选课
     */
//    @PostMapping("/selectCourse")
//    public  Res  selectCourse(@RequestBody Map<String, Object> requestData) {
//        int courseId = (int) requestData.get("courseId");
//        int studentId = (int) requestData.get("studentId");
//        if (coursesService.getChosen(courseId)<)
////        if (jobService.getJob(j_id).isEmpty() || jobService.getCustom(c_id).isEmpty()) {
////            return Response.err("请确保j_id或者c_id存在");
////        }
////        else {
////            if (jobService.examineJob(j_id, c_id).isEmpty()) {
//                coursesService.insertSelect(courseId, studentId);
//                return Res.ok();
//
//        }



}

